package org.dev.biz_login

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.activity_register.*
import org.dev.`as`.project.common.http.ApiFactory
import org.dev.`as`.project.common.ui.component.HiBaseActivity
import org.dev.biz_login.api.AccountApi
import org.dev.hi.library.restful.HiCallback
import org.dev.hi.library.restful.HiResponse
import org.dev.hi.library.util.HiStatusBar

/**
 * Author WenPing
 * CreateTime 2021/5/9.
 * Description:注册页面
 */
@Route(path = "/account/registration")
class RegistrationActivity : HiBaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        HiStatusBar.setStatusBar(this, true, translucent = false)

        setContentView(R.layout.activity_register)

        action_back.setOnClickListener {
            onBackPressed()
        }

        action_submit.setOnClickListener {
            submit()
        }


    }

    /**
     * 点击开始注册
     */
    private fun submit() {
        val orderId = input_item_orderId.getEditTextView().text.toString()
        val moocId = input_item_moocId.getEditTextView().text.toString()

        val userName = input_item_username.getEditTextView().text.toString()
        val pwd = input_item_pwd.getEditTextView().text.toString()
        val pwdSecond = input_item_pwd_check.getEditTextView().text.toString()

        if (TextUtils.isEmpty(orderId) ||
            TextUtils.isEmpty(moocId) ||
            TextUtils.isEmpty(userName) ||
            TextUtils.isEmpty(pwd) ||
            (!TextUtils.equals(pwd, pwdSecond))
        ) {
            Toast.makeText(this,"注册信息存在异常，请确认后重试",Toast.LENGTH_SHORT).show()
            return
        }

        /**
         * 调用注册接口，注册成功后
         */
        ApiFactory.create(AccountApi::class.java)
            .register(
                userName,
                pwd,
                moocId,
                orderId
            ).enqueue(object : HiCallback<String> {
                override fun onSuccess(hiResponse: HiResponse<String>) {
                    if (hiResponse.code == HiResponse.SUCCESS) {
                        // 注册成功 携带userName 回到登录页面
                        val intent = Intent()
                        intent.putExtra("userName", userName)
                        setResult(Activity.RESULT_OK, intent)
                        finish()
                    } else {
                        showToast(hiResponse.msg)
                    }
                }

                override fun onFailed(throws: Throwable) {
                    showToast(throws.message)
                }
            })


    }

}